//
//  MGMSafeObject.m
//  max-mglive-main
//
//  Created by 刘律宏 on 2017/5/23.
//  Copyright © 2017年 MGMive. All rights reserved.
//

#import "MSSafeObject.h"

@implementation MSSafeObject

+ (BOOL)isNull:(id)object
{
	
	if (object == nil) {
		return YES;
	}
	
	if ([object isEqual:[NSNull null]]) {
		return YES;
	}
	
	if ([object isKindOfClass:[NSNull class]]) {
		return  YES;
	}
	
	if ([object isKindOfClass:[NSArray class]]) {
		return ((NSArray *)object).count == 0;
	}

	if ([object isKindOfClass:[NSDictionary class]]) {
		return ((NSDictionary *)object).count == 0;
	}
	
	
	if ([object isKindOfClass:[NSString class]]) {
		if (((NSString *)object).length == 0) {
			return YES;
		}
		if ([object isEqualToString:@"(null)"] ||
			[object isEqualToString:@"<null>"] ||
			[object isEqualToString:@"<nil>"] ||
			[object isEqualToString:@""]) {
			return YES;
		}
	}
	
	return NO;
	
	
//    // 判断是否为空串
//    if ([object isEqual:[NSNull null]]) {
//        return YES;
//    }
//    else if ([object isKindOfClass:[NSNull class]])
//    {
//        return YES;
//    }
//    else if (object==nil){
//        return YES;
//    }else if ([object isKindOfClass:[NSString class]]){
//        if ([object isEqualToString:@"(null)"] ||
//            [object isEqualToString:@"<null>"] ||
//            [object isEqualToString:@"<nil>"] ||
//            [object isEqualToString:@""]) {
//            return YES;
//        }
//    }
	
}

+ (NSDictionary *)dictionaryInfo:(id)object
{
    if (!object) {
        return @{};
    }
    if ([object isKindOfClass:[NSDictionary class]]) {
        return object;
    }
	return @{};
}

+ (NSString *)stringInfo:(id)object
{
    if ([MSSafeObject isNull:object]) {
        return @"";
    }
    if ([object isKindOfClass:[NSString class]]) {
        return object;
    }
	return [object description];
}

+ (NSArray *)ArrayInfo:(id)object
{
    if ([object isKindOfClass:[NSArray class]]) {
        return object;
    }
	return @[];
}

+ (BOOL)boolValue:(id)object
{
    if ([MSSafeObject isNull:object]) {
        return NO;
    }
    if ([object intValue] == 0) {
        return NO;
    }
	return YES;
}

@end

void dispatch_sync_mainqueue(dispatch_block_t block)
{
    if ([NSThread isMainThread]) {
        block();
    } else {
        dispatch_sync(dispatch_get_main_queue(), ^{
            block();
        });
    }
}
